package model

import (
	"context"
	"github.com/LyricTian/gin-admin/v7/internal/app/model/gormx/entity"
	"github.com/LyricTian/gin-admin/v7/internal/app/schema"
	"github.com/LyricTian/gin-admin/v7/pkg/errors"
	"github.com/jinzhu/gorm"
)

func (a *GroupMember) GetGroupMember(ctx context.Context, params schema.GroupMemberQueryParam,
	opts ...schema.GroupMemberQueryOptions) (*schema.UserInfoQueryResult, error) {

	//读取群关联的视频
	subQuery := entity.GetGroupMemberDB(ctx, a.DB).
		Select("user_id").
		Where("group_id = ?", params.GroupId).
		SubQuery()
	db := entity.GetUserInfoDB(ctx, a.DB).Where("id IN ?", subQuery)
	//读取视频列表
	// todo 加群时，将进群时间同步到用户信息里面
	opt := a.getQueryOption(opts...)
	opt.OrderFields = append(opt.OrderFields, schema.NewOrderField("in_group_time", schema.OrderByDESC))
	db = db.Order(ParseOrder(opt.OrderFields))

	var list entity.UserInfos
	pr, err := WrapPageQuery(ctx, db, params.PaginationParam, &list)
	if err != nil {
		return nil, errors.WithStack(err)
	}
	qr := &schema.UserInfoQueryResult{
		PageResult: pr,
		Data:       list.ToSchemaUserInfos(),
	}

	return qr, nil

}

// todo 添加一天
func (a *GroupMember) AddReciteDay(ctx context.Context) {
	db:=entity.GetGroupMemberDB(ctx,a.DB)
	db.Update("recite_day", gorm.Expr("recite_day + ?", 1))
	//GetSqlX(a.DB).Exec("update g_group_member set recite_day=recite_day+1 ")
}
